package com.dlc.felear.xiaoensale.helper;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.utils.HttpApi;
import com.dlc.felear.xiaoensale.utils.JasyptUtil;
import com.dlc.felear.xiaoensale.utils.MyLog;
import com.itdlc.android.library.utils.APPDidUtil;
import com.itdlc.android.library.utils.FileUtil;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import org.simple.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class ReqAdData {
    private static final String TAG = "ReqAdData";

    /**
     * 刷脸调用开门
     *
     * @param openid
     */
    public static void reqOpenDoor(String openid) {
        Map<String, Object> temp = new HashMap<>();
        temp.put("deviceId", APPDidUtil.getAppDid());
        temp.put("openId", openid);
        String sign = JasyptUtil.getInstance().getSign(temp);
        temp.put("sign", sign);
//        MyLog.e(TAG, "openId=" + openid);
        if (checkParamEmpty(temp)) {
            return;
        }
        OkHttpManager.HttpPostJson(HttpApi.OPEN_DOOR, temp, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
//                MyLog.e(TAG, "reqOpenDoor" + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
//                MyLog.e(TAG, "reqOpenDoor" + response);
                EventBus.getDefault().post(response, Constant.WX_REQ_OPEN_DOOR);
            }
        });
    }

    /**
     * 获取刷脸设备认证
     *
     * @param rawdata
     */
    public static void sendRawData(String rawdata) {
        Map<String, Object> temp = new HashMap<>();
        temp.put("deviceId", APPDidUtil.getAppDid());
        temp.put("rawdata", rawdata);
        String sign = JasyptUtil.getInstance().getSign(temp);
        temp.put("sign", sign);
        if (checkParamEmpty(temp)) {
            return;
        }
        OkHttpManager.HttpPostJson(HttpApi.AUTHINFO_URL, temp, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
//                MyLog.e(TAG, "获取RawData" + e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
//                MyLog.e(TAG, "获取RawData" + response);
                EventBus.getDefault().post(response, Constant.WX_REQ_AUTH_INFO);
            }
        });
    }


    /**
     * 获取二维码
     */
    public static void getDeviceQr() {
        String name = "mp_" + APPDidUtil.getAppDid() + ".png";
        String url = HttpApi.PIC_URL + name;
        if (FileUtil.checkFileIsExist(Constant.PIC_PATH + name)) {
//            MyLog.e(TAG, "文件已经缓存");
            EventBus.getDefault().post(Constant.PIC_PATH + name, Constant.WX_REQ_QR_CODE);
            return;
        }
        OkHttpManager.HttpGet(url, new FileCallBack(Constant.PIC_PATH, name) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(File response, int id) {
//                MyLog.e(TAG, "下载完成 file=");
                if (response != null) {
                    MyLog.e(TAG, "file=" + response.getAbsolutePath());
                    EventBus.getDefault().post(response.getAbsolutePath(), Constant.WX_REQ_QR_CODE);
                }
            }


            @Override
            public void inProgress(float progress, long total, int id) {
                if (progress == 1) {
//                    MyLog.e(TAG, "下载完成");
                }
            }

        });
    }

    /**
     * 判断用户是否签约
     *
     * @param openId
     */
    public static void queryContract(String openId) {
        Map<String, Object> temp = new HashMap<>();
        temp.put("deviceId", APPDidUtil.getAppDid());
        temp.put("openId", openId);
        String sign = JasyptUtil.getInstance().getSign(temp);
        temp.put("sign", sign);
        if (checkParamEmpty(temp)) {
            return;
        }
        OkHttpManager.HttpPostJson(HttpApi.QUERY_CONTRACT, temp, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
//                MyLog.e(TAG, "获取queryContract" + e.getMessage());
                String response = "{\"code\":400,\"msg\":\"网络超时\",\"data\":null,\"time\":1563953628374}";
                EventBus.getDefault().post(response, Constant.WX_REQ_QUERY_CONTRACT);
            }

            @Override
            public void onResponse(String response, int id) {
//                MyLog.e(TAG, "获取queryContract" + response);
                EventBus.getDefault().post(response, Constant.WX_REQ_QUERY_CONTRACT);
            }
        });
    }

    /**
     * 检查请求参数是否为空
     *
     * @param params
     * @return
     */
    private static boolean checkParamEmpty(Map<String, Object> params) {
        for (String key : params.keySet()) {
            if (params.get(key) == null) {
                return true;
            }
        }
        return false;
    }


}
